package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * java配置类
 *      创建死信交换机
 *      创建队列
 *      创建RoutingKey
 *      绑定Binding
 */

@Configuration
public class DeadLetterMqConfig {

    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";

    //交换机
    @Bean
    public Exchange deadLetterExchange(){
        //return new DirectExchange(exchange_dead);
        return ExchangeBuilder.directExchange(exchange_dead).build();
    }

    //死信队列
    @Bean
    public Queue deadLetterQueue(){
        Map<String, Object> arguments = new HashMap<>();
        //1.转发的死信交换机是多少
        arguments.put("x-dead-letter-exchange",exchange_dead);
        //2.转发的routingKey是谁
        arguments.put("x-dead-letter-routing-key",routing_dead_2);
        //3.TTL是多少 此处是全局设置
        arguments.put("x-message-ttl",10*1000);
        return QueueBuilder.durable(queue_dead_1).withArguments(arguments).build();
    }

    //正常队列
    @Bean
    public Queue queue(){
        return QueueBuilder.durable(queue_dead_2).build();
    }

    //绑定死信队列
    @Bean
    public Binding bindingDeadLetterQueue(){
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange())
                .with(routing_dead_1).noargs();
    }

    //绑定普通队列
    @Bean
    public Binding bindingQueue(){
        return BindingBuilder.bind(queue()).to(deadLetterExchange())
                .with(routing_dead_2).noargs();
    }

}
